let Local = function (socket) {
    const INTERVAL = 500;
    let game;
    let timer = null;
    let time = 0;
    let timeCount = 0;

    let bindKeyEvent = function () {
        document.onkeydown = function (e) {
            if (e.code === 'ArrowUp') { // ↑
                game.rotate();
                socket.emit("rotate");
            } else if (e.code === 'ArrowRight') { // →
                game.right();
                socket.emit("right");
            } else if (e.code === 'ArrowDown') { // ↓
                game.down();
                socket.emit("down");
            } else if (e.code === 'ArrowLeft') { // ←
                game.left();
                socket.emit("left");
            } else if (e.code === 'Space') { // 空格
                game.fall();
                socket.emit("fall");
            }
        }
    };

    let generateBottomLine = function (lineNum) {
        let lines = [];
        for (let i = 0; i < lineNum; i++) {
            let line = [];
            for (let j = 0; j < 10; j++) {
                line.push(Math.ceil(Math.random() * 2) - 1);
            }
            lines.push(line);
        }
        return lines;
    };

    let timeFunc = function () {
        timeCount += 1;
        if (timeCount === 1000 / INTERVAL) {
            timeCount = 0;
            time = time + 1;
            game.setTime(time);
            socket.emit("time", time);
        }
    };

    let move = function () {
        timeFunc();
        if (!game.down()) {
            game.fixed();
            socket.emit("fixed");
            let line = game.checkClear();
            if (line) {
                game.addScore(line);
                socket.emit("line", line);
                if (line >= 4) {
                    let bottomLines = generateBottomLine(1);
                    socket.emit("bottomLines", bottomLines);
                }
            }
            let gameOver = game.checkGameOver();
            if (gameOver) {
                game.gameOver(false);
                document.getElementById("remote-game-over").innerHTML = "你赢了";
                socket.emit("lose");
                stop();
            } else {
                let type = generateType();
                let dir = generateDir();
                game.performNext(type, dir);
                socket.emit("next", {
                    "type": type,
                    "dir": dir
                });
            }
        } else {
            socket.emit("down");
        }
    };

    let generateType = function () {
        return Math.ceil(Math.random() * 7) - 1;
    };

    let generateDir = function () {
        return Math.ceil(Math.random() * 4) - 1;
    };

    let start = function () {
        let domes = {
            gameDiv: document.getElementById("local-game"),
            nextDiv: document.getElementById("local-next"),
            timeDiv: document.getElementById("local-time"),
            scoreDiv: document.getElementById("local-score"),
            resultDiv: document.getElementById("local-game-over"),
        };
        game = new Game();
        let type = generateType();
        let dir = generateDir();
        game.init(domes, type, dir);
        socket.emit("init", {
            "type": type,
            "dir": dir
        });
        bindKeyEvent();
        let nextType = generateType();
        let nextDir = generateDir();
        game.performNext(nextType, nextDir);
        socket.emit("next", {
            "type": nextType,
            "dir": nextDir
        });
        timer = setInterval(move, INTERVAL);
    };

    let stop = function () {
        if (timer) {
            clearInterval(timer);
            timer = null;
        }
        document.onkeydown = null;
    };

    socket.on("start", function () {
        document.getElementById("waiting").innerHTML = "";
        start();
    });

    socket.on("lose", function () {
        game.gameOver(true);
        stop();
    });

    socket.on("leave", function () {
        document.getElementById("local-game-over").innerHTML = "对方掉线";
        document.getElementById("remote-game-over").innerHTML = "已掉线";
        stop();
    });

    socket.on("bottomLines", function (data) {
        game.addTailLines(data);
        socket.emit("addTailLines", data);
    });
};
